#include <math.h>
#include <windows.h>
#include <stdio.h>

#include "config.h"
#include "event.h"
#include "util.h"
#include "obd_pid.h"

int
main(int argc, char **argv)
{
	FILE *fpcfg, *fplog;
	char *cfg_file, *log_file;
	int r;
	struct config config;
	uint32_t next, file_size;
	int session_num, event_num;
	int cur_session;
	double time;
	int i;

	if (argc != 4) {
		fprintf(stderr, "usage: %s config_file log_file "
		    "session_num\n", argv[0]);
		exit(1);
	}

	cfg_file = argv[1];
	log_file = argv[2];
	session_num = atoi(argv[3]);
	event_num = atoi(argv[4]);

	if (event_num >= NUM_LOG_SLOTS || event_num < 0) {
		fprintf(stderr, "error: invalid event number\n");
		exit(1);
	}

	fpcfg = fopen(cfg_file, "rb");
	if (fpcfg == NULL) {
		fprintf(stderr, "error: could not open %s\n", cfg_file);
		exit(1);
	}

	r = fread(&config, 1, sizeof(config), fpcfg);

	if (!valid_config(&config)) {
		fprintf(stderr, "error: invalid config read.\n");
		exit(1);
	}
	
	fplog = fopen(log_file, "rb");
	if (fplog == NULL) {
		fprintf(stderr, "error: could not open %s\n", log_file);
		exit(1);
	} 
	fseek(fplog, 0, SEEK_END);
	file_size = ftell(fplog);
	rewind(fplog);

	cur_session = 0;
	while (cur_session <= session_num) {
		r = fread(&next, 1, sizeof(next), fplog);
		if (r != sizeof(next)) {
			fprintf(stderr, "error: could not read next"
			    " address (session # too big)\n");
			exit(1);
		}

		if (cur_session == session_num)
			break;

		if (fseek(fplog, next, SEEK_SET) == -1) {
			fprintf(stderr, "error: could not seek next"
			    " address (session # too big)\n");
			exit(1);
		}

		++cur_session;
	}

	if (ftell(fplog) == file_size) {
		fprintf(stderr, "error: invalid session number\n");
		exit(1);
	}

	if (config.event[event_num].interval == 0) {
		fprintf(stderr, "error: invalid event number\n");
		exit(1);
	}

	printf("# Time (s)");

	for (i = 0; i < NUM_LOG_SLOTS; ++i) {
		printf("\t");

		if (config.event[i].interval == 0)
			continue;

		printf("%s (%s)", pid_name[config.event[i].pid],
		    pid_units(config.event[i].pid));
	}

	printf("\n");

	time = 0;
	while (ftell(fplog) < next) {
		uint32_t data;
		double value;

#if 0
		printf("%d:%d:%.2f", (int) floor(time / 3600.0), (int) floor(time / 60.0), ((int)(time * 10.0) % 600) / 10.0);
#else
		printf("%.2f", time);
#endif
		time += 0.5;

		for (i = 0; i < NUM_LOG_SLOTS; ++i) {
			if (config.event[i].interval == 0)
				continue;
			
			config.event[i].count++;

			printf("\t");

			if (config.event[i].count == config.event[i].interval) {
				config.event[i].count = 0;

				/* read the data, inc addr */
				data = 0;
				r = fread(&data, 1, PID_LENGTH(config.event[i].pid), fplog);
				if (r == 0) {
					fprintf(stderr, "error: read\n");
					exit(1);
				}

				if (PID_LENGTH(config.event[i].pid) == 2) {
					data = htons(data);
				} else if (PID_LENGTH(config.event[i].pid) == 4) {
					data = htonl(data);
				}

				value = conv_pid_data(config.event[i].pid, data);

				printf("%.02f", value);
			} else {
				printf("?0");
			}
		}

		printf("\n");
	}

	fclose(fpcfg);
	fclose(fplog);

	return 0;
}
